package bitmap;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class testBMP
{
	public static void main(String[] args) throws Exception
	{

		int val = 13|17<<8|23<<16;
		int blue = val/(1<<16);
		int  green = (val-(blue<<16))/(1<<8);
		int red = (val-(blue<<16)-(green<<8));

		
//		int lenght=201;
//		int width=201;
//		int[][] rgbValues = new int[lenght][width];
//		for(int i=0;i<width;i++){
//			for(int j=0;j<lenght;j++){
//				if (i%5==0 || j%5==0){
//					rgbValues[i][j]=BMP.getColor(0, 0, 0);
//				}
//				else{
//					rgbValues[i][j]=BMP.getColor(0, 0, 0);
//				}
//			}
//		}
//		
//		BMPText bt = new BMPText();
//		rgbValues=bt.print("ciao bello", rgbValues, BMP.MAGENTA, 2);
//		BMP bmp = new BMP();
//		bmp.saveBMP("c:/tmp/testTXT.bmp",rgbValues);
		
		
		int lenght=5;
		int width=lenght;
		BMPobj img = new  BMPobj(lenght,width);
		img.fill(Color.GREY);
//		int xx =50;
//		int yy =50;
//		img.point(xx, yy, Color.BLU.getRgbInt());
//		img.drawCircle(xx, yy, 30, Color.RED);
//		
//		img.fillCircle(120, 120, 30, Color.RED,null);
//
//		img.drawLine(10, 10, 20, 150, Color.BLACK);
		

		//img.drawSquare(20, 20, 60, 90, Color.WHITE);
		
//		img.point(2, 2, Color.BLU.getRgbInt());
//		img.point(2, 3, Color.BLU.getRgbInt());
//		img.point(2, 4, Color.BLU.getRgbInt());
//		img.point(2, 5, Color.BLU.getRgbInt());

		//img.fillSquare(50, 50, 100, 100, Color.RED, null, true);

		//img.fillSquare(80, 80, 140, 140, Color.GREEN, null, true);
/*
		int r=8;
		int c=8;
		int rRatio= lenght/r;
		int cRatio= lenght/c;
		int radius = rRatio/3;
		
		for(int x=0;x<r;x++){
			for(int y=0;y<c;y++){
				drawTriplets(x*rRatio+rRatio/2, y*cRatio+cRatio/2, radius, 1, img);
			}
		}*/

//		img.point(0, 0, Color.BLU);
//		img.point(0, 1, Color.RED);;
//		img.point(0, 2, Color.GREEN);
//		img.point(1, 0, Color.CYAN);
//		img.point(1, 1, Color.MAGENTA);
//		img.point(1, 2, Color.YELLOW);
//		img.point(2, 0, Color.WHITE);
//		img.point(2, 1, Color.BLACK);
//		img.point(2, 2, Color.GREY);
//		drawTriplets(45*mul, 50*mul, 40*mul, 1, img);
//		drawTriplets(180*mul, 50*mul, 40*mul, 0.9, img);
//		drawTriplets(315*mul, 50*mul, 40*mul, 0.8, img);
//
//
//		drawTriplets(45*mul, 200*mul, 40*mul, 0.7, img);
//		drawTriplets(180*mul, 200*mul, 40*mul, 0.6, img);
//		drawTriplets(315*mul, 200*mul, 40*mul, 0.5, img);
//
//
//		drawTriplets(45*mul, 350*mul, 40*mul, 0.4, img);
//		drawTriplets(180*mul, 350*mul, 40*mul, 0.3, img);
//		drawTriplets(315*mul, 350*mul, 40*mul, 0.2, img);
		
		
		
		BMPSave bmp = new BMPSave();
		String path="c:/tmp/test5Grey.bmp";
		bmp.saveBMP(path, img.image);
		
		File input = new File(path);  

		//Read the file to a BufferedImage  
		BufferedImage imageB = ImageIO.read(input);

		//Create a file for the output  
		//File output = new File(path+".jpg");  
		
		Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
		ImageWriter writer = (ImageWriter)iter.next();
		ImageWriteParam iwp = writer.getDefaultWriteParam();
		iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		iwp.setCompressionQuality(1.0f); 
		
		
		File file = new File(path+".jpg");
		FileImageOutputStream output = new FileImageOutputStream(file);
		writer.setOutput(output);
		IIOImage image = new IIOImage(imageB, null, null);
		writer.write(null, image, iwp);
		writer.dispose();
		System.out.println("done");
		
	}
	
	public static void drawTriplets(int x, int y, int r, double gamma, BMPobj img)
	{
		int step = (int) (r/1.5);
		img.fillCircle(x, y, r, new Color(255,0,0,gamma),null, true);
		img.fillCircle(x+step, y, r, new Color(0,255,0,gamma),null, true);	
		img.fillCircle(x+step*2, y, r, new Color(0,0,255,gamma),null, true);
	}
}
